<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <!-- 面包屑导航区域 -->
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
        <el-row :gutter="20">
            <el-col :span="8">
                <el-input 
                clearable
                @keydown.native.enter="searchVal"
                @change="changeVal"
                @clear="clearVal"
                v-model.trim="goodsPaginationData.query" 
                placeholder="请输入内容">
                    <el-button 
                    slot="append" icon="el-icon-search"
                    @click="searchVal"></el-button>
                </el-input>
            </el-col>
            <el-col :span="2">
                <el-button 
                 type="primary"
                 @click="goAddGoods">添加商品</el-button>
            </el-col>
        </el-row>
        <el-table
        :data="goodsTableData.goods"
        stripe
        border
        style="width: 100%">
            <el-table-column
            label="#"
            width="50px"
            type="index">
            </el-table-column>
            <el-table-column
            prop="goods_name"
            label="商品名称"
            min-width="300px">
            </el-table-column>
            <el-table-column
            prop="goods_price"
            label="商品价格(元)"
            width="102px">
            </el-table-column>

            <el-table-column
            prop="goods_number"
            label="商品重量"
            width="80px">
            </el-table-column>
            <el-table-column
            prop="upd_time"
            label="创建时间"
            width="170px">
            </el-table-column>
            <el-table-column
            label="操作"
            width="120px">
                <template scope="{row}">
                    <el-button 
                    size="mini"
                    type="primary" icon="el-icon-edit">
                    </el-button>
                    <el-button 
                    size="mini"
                    type="danger" icon="el-icon-delete"
                    @click="delGoods(row)">
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页部分 -->
        <el-pagination
            background
            @current-change="currentChange"
            @size-change="pagesizeChange"
            layout="total,sizes,prev, pager, next,jumper"
            :total="goodsTableData.total"
            :page-sizes="[5, 10, 20, 30]">
        </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
    name:'List',
    data(){
        return {
            // table表格数据
            goodsTableData:{},
            // 定义一个数据用来存储当前表格分页得数据
            goodsPaginationData:{
                query:'',
                pagenum:1,
                pagesize:10
            }
        }
    },
    methods:{
        // 监听单击添加商品后跳转到add组件
        goAddGoods(){
            this.$router.push('/goods/add')
        },
        // 发起获取商品列表得请求
        async getGoodsList(){
            const {data:res} = await this.$axios.get('goods',{
                params:this.goodsPaginationData
            });
            if(res.meta.status!==200) return this.$message.error(res.meta.msg);
            
            res.data.goods.forEach(item=>{
                item.upd_time = this.transformDate(item.upd_time)
            })
            console.log(res);

            this.goodsTableData = res.data;
        },
        // 监听用户改变每页显示多少条数据
        async pagesizeChange(size){
            this.goodsPaginationData.pagesize = size;
     
            this.getGoodsList();
        },
        // 监听用户改变页数时触发
        currentChange(pagenum){
            this.goodsPaginationData.pagenum = pagenum;
            this.getGoodsList();
        },

        // 监听用户点击搜索按钮
        searchVal(){
            this.getGoodsList();
        },
        // 监听用户单击清除输入框内容后
        clearVal(){
            this.getGoodsList();
        },
        // 监听当用户输入框发生改变时
        changeVal(val){
            if(!val) this.getGoodsList();
        },
        // 监听当用户按下删除商品后
        delGoods(row){
            this.$messageBox.confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then(async () => {
                const {data:res} = await this.$axios.delete(`goods/${row.goods_id}`)
                console.log(res);

                this.getGoodsList();
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });

                }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
                });
        },


        // 时间戳转换为时间格式
        transformDate(data){
            let date = new Date(data);
            let YY = date.getFullYear() + '-';
            let MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
            let DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
            let hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
            let mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
            let ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
            return YY + MM + DD +" "+hh + mm + ss;
        },
    },
    created() {
        this.getGoodsList();
    },
}
</script>

<style scoped lang="less">

</style>